<template>
  <el-dialog
    :title="showTitle"
    :visible="dialogVisible"
    @close="handleClose"
  >
    <!-- 匿名插槽 -->
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in peoples" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button type="primary" size="small" @click="handleConfirm">确定</el-button>
      <el-button size="small" @click="handleClose">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reqGetEmployeeSimple } from '@/api/employees'
import { reqAddDepartments, reqGetDepartDetail, reqUpdateDepartments } from '@/api/departments'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    depts: {
      type: Array,
      required: true
    },
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    const validateName = (rules, value, callback) => {
      let arr
      if (this.form.id) {
        if (this.nodeData.name === value) {
          callback()
          return
        }
        arr = this.depts.filter(item => item.pid === this.nodeData.pid)
      } else {
        arr = this.depts.filter(item => item.pid === this.nodeData.id)
      }
      //   console.log(JSON.parse(JSON.stringify(arr)))
      const isRepeat = arr.some(item => item.name === value)
      isRepeat ? callback(new Error('同级部门中禁止出现重复部门')) : callback()
      // if (this.form.id && this.nodeData.name === value) {
      //   callback()
      //   return
      // }
      // const isRepeat = this.depts.some(item => item.name === value)
      // isRepeat ? callback(new Error('部门名称不允许重复')) : callback()
    }
    const validateCode = (rules, value, callback) => {
      // 编辑的时候允许编码重复
      if (this.form.id && this.nodeData.code === value) {
        callback()
        return
      }
      const isRepeat = this.depts.some(item => item.code === value)
      isRepeat ? callback(new Error('部门编码不允许重复')) : callback()
    }
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '请填写部门名称', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validateName, trigger: 'blur' }

        ],
        code: [
          { required: true, message: '请填写部门编码', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validateCode, trigger: 'blur' }

        ],
        manager: [
          { required: true, message: '请填写部门负责人', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '请填写部门介绍', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门编码要求1-300个字符', trigger: ['blur', 'change'] }

        ]
      },
      peoples: []
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getEmployeeSimple() // 这个是每次直接获取一次数据，不会每次打开都获取数据
  },
  methods: {
    handleConfirm() {
      this.$refs.form.validate(async(data) => {
        if (data) {
          if (this.form.id) {
            await reqUpdateDepartments(this.form)
            this.$message.success('编辑成功')
          } else {
            // console.log('发送请求')
            await reqAddDepartments({
              ...this.form,
              pid: this.nodeData.id
            })
            // 提示
            this.$message.success('添加部门成功')
          }
          // 关闭弹层
          this.handleClose()
          // 子传父添加
          this.$emit('add-dialog')
        }
      })
    },
    // handleOpen() {
    //   this.getEmployeeSimple()
    // }, //业务需求的话我们每次打开需要最新的数据
    handleClose() {
      this.$emit('update:dialogVisible', false)
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        id: ''
      }
      this.$refs.form.resetFields()
    },
    async getEmployeeSimple() {
      const res = await reqGetEmployeeSimple()
      // console.log(res)
      this.peoples = res.data
    },
    async getDepartDetail(id) {
      const { data } = await reqGetDepartDetail(id)
      console.log(data)
      this.form = { ...data }
    }
  }
}
</script>

<style>

</style>
